/*action create by pionner code builder*/
package com.pioneer.app.dev.department;

import java.io.Serializable;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.dom4j.Document;

import com.app.util.Dom4jUtil;
import org.hibernate.Transaction;

import java.util.ArrayList;
import java.util.List;


import com.opensymphony.xwork2.Action;
import com.pioneer.app.dev.osuser.user.Userlist;
import com.pioneer.app.dev.osuser.user.UserlistDAO;

import java.io.Serializable;

public class DepartmentAction implements Action, ServletRequestAware,ServletResponseAware,Serializable {

	// key
	private java.lang.Integer id;

	private java.lang.Integer[] ids;
	private Department department;
	private java.lang.String code;
	private java.lang.String name;
	private java.lang.String remark;
	private java.lang.Integer pid;
	private List objects=null;
	private String condition=null;
	private String rtPage=null;
	private HttpServletResponse response;
	private HttpServletRequest request;
	/** Ĭ�Ϲ��췽�� */
	public DepartmentAction(){
	}
	/** full constructor */
	public DepartmentAction(java.lang.String code,java.lang.String name,java.lang.String remark,java.lang.Integer pi){
		this.code=code;
		this.name=name;
		this.remark=remark;
		this.pid=pid;

	}
	public void setId(java.lang.Integer id){
		this.id=id;
	}
	public java.lang.Integer getId(){
		return id;
	}

	public void setIds(java.lang.Integer[] ids){
		this.ids=ids;
	}
	public java.lang.Integer[] getIds(){
		return ids;
	}

	public void setDepartment(Department department){
		this.department=department;
	}
	public Department getDepartment(){
		return department;
	}

	public void setCode(java.lang.String code){
		this.code=code;
	}
	public java.lang.String getCode(){
		return code;
	}

	public void setName(java.lang.String name){
		this.name=name;
	}
	public java.lang.String getName(){
		return name;
	}

	public void setRemark(java.lang.String remark){
		this.remark=remark;
	}
	public java.lang.String getRemark(){
		return remark;
	}

	public void setPid(java.lang.Integer pid){
		this.pid=pid;
	}
	public java.lang.Integer getPid(){
		return pid;
	}

	public List getObjects() {
		return objects;
	}

	public void setObjects(List objects) {
		this.objects = objects;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String execute() throws Exception {
		return "SUCESS";
	}

	public String doList(){
		DepartmentDAO dao=new DepartmentDAO();
		this.objects=dao.findByCondition(condition);
		
		if(null==this.rtPage){
			this.rtPage="list";
		}
		return this.rtPage;
	}

	public String doAdd(){
		return "add";
	}

	public String doEdit(){
		DepartmentDAO dao=new DepartmentDAO();
		this.department=dao.findById(id);
		dao.getSession().close();
		this.sedObj();;
		return "edit";
	}

	public String doSave(){
		Transaction tx=null;
		try {
			this.buildObj();
			DepartmentDAO dao=new DepartmentDAO();
			tx=dao.getSession().beginTransaction();
			if(null==this.department.getId()){
//			insert a new object
			//gave the object id by database 
				dao.add(department);
			}else{
//			update the object
				dao.getSession().clear();
				dao.update(department);
			}
			tx.commit();
			dao.getSession().close();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return "error";
		}
		if(null==this.rtPage){
			this.rtPage="list";
		}
		return this.rtPage;
	}

	
	public String doDetail(){
			DepartmentDAO dao=new DepartmentDAO();
		this.department=dao.findById(id);
		this.sedObj();;
		dao.getSession().close();
		return "detail";
	}
	public String doDeleteSingle(){
		try{
			DepartmentDAO dao=new DepartmentDAO();
			
			
			List delete=findLowerDepart(id,dao);
			
			ids =new Integer[delete.size()+1];			
			for(int i=0;i<delete.size();i++){
				ids[i]=((Department)delete.get(i)).getId();
			}
			ids[delete.size()]=id;
			dao.deleteObjs(ids);
			dao.getSession().close();			
			return "list";
		}catch(Exception e){
			e.printStackTrace();
			return "list";
		}
	}
	public String doDelete(){
		try{
			DepartmentDAO dao=new DepartmentDAO();
			dao.deleteObjs(ids);
			dao.getSession().close();
			return "list";
		}catch(Exception e){
			return "error";
		}
	}
	public List findLowerDepart(Integer id,DepartmentDAO dao){
		condition="pid='"+id+"'";
		
		List temp= dao.findByCondition(condition);
		if(temp.size()>0){
			int size=temp.size();
			for(int i=0;i<size;i++){
				Integer tempId =((Department)temp.get(i)).getId();
				List tempList=findLowerDepart(tempId,dao);
				temp.addAll(tempList);
			}
		}		
		return temp;
	}
	public String doDeptUser(){
		try {
			response.setHeader("Pragma","No-cache");
			response.setHeader("Cache-Control","no-cache");
			response.setDateHeader("Expires",1); 

			String pid=request.getParameter("pid");
			UserlistDAO dao=new UserlistDAO();
			Document doc=dao.getUserList("departmentId="+pid);
			this.response.setContentType("text/xml");
			dao.getSession().close();
			Dom4jUtil.writeDocToOut(doc, "utf-8", this.response.getOutputStream());
		} catch (Exception e) {
		}
		return null;
	}
	public String doTreeNode(){
		try {
			response.setHeader("Pragma","No-cache");
			response.setHeader("Cache-Control","no-cache");
			response.setDateHeader("Expires",1); 

			String pid=request.getParameter("pid");
			DepartmentDAO dao=new DepartmentDAO();
			Document doc=dao.getTreeNode(pid);
			dao.getSession().close();
			this.response.setContentType("text/xml");
			Dom4jUtil.writeDocToOut(doc, "utf-8", this.response.getOutputStream());
		} catch (Exception e) {
		}
	return null;
	}
 

	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}

	private void buildObj(){
		this.department=new Department();
		this.department.setId(this.id);
		this.department.setCode(this.code);
		this.department.setName(this.name);
		this.department.setRemark(this.remark);
		this.department.setPid(this.pid);
	}
	private void sedObj(){

		if(null!=this.department){
			this.id=this.department.getId();
			this.code=this.department.getCode();
			this.name=this.department.getName();
			this.remark=this.department.getRemark();
			this.pid=this.department.getPid();

		}
	}
	public String getRtPage() {
		return rtPage;
	}
	public void setRtPage(String rtPage) {
		this.rtPage = rtPage;
	}
}